/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef __DF_DATA_H__
#define __DF_DATA_H__

#include <stdint.h>

/** Duration of guard period in CTE */
#define DFE_GUARD_PERIOD_US 4
/** Duration of reference period in CTE in microseconds */
#define DFE_REF_PERIOD_US 8
/** Duration of reference period in CTE in nanoseconds */
#define DFE_REF_PERIOD_NS (1000 * DFE_REF_PERIOD_US)
/** Duration of a antenna switching period */
#define DFE_ANT_SWTICHING_PERIOD_US ((8 * (CONFIG_BT_CTLR_DFE_NUMBER_OF_8US)) \
		- ((DFE_REF_PERIOD_US) + (DFE_GUARD_PERIOD_US)))
/** Time between antenna consecutive switches in nanoseconds */
#define DFE_ANT_SWITCHING_PERIOD_NS (1000 * (DFE_ANT_SWTICHING_PERIOD_US))

/** Number of samples that will be collected in antenna switching period
 *
 * @note the number is greater than real one because there is a delay in sampling
 * after end of reference period
 * */
#define DFE_SAMPLES_NUM ((DFE_ANT_SWITCHING_PERIOD_NS) / (CONFIG_BT_CTLR_DFE_SAMPLE_SPACING_NS))

/** Number of samples collected in reference period */
#define DFE_REF_SAMPLES_NUM ((DFE_REF_PERIOD_NS) / (CONFIG_BT_CTLR_DFE_SAMPLE_SPACING_REF_NS))

/** Total number of samples collected during whole CTE receive */
#define DFE_TOTAL_SAMPLES_NUM ((DFE_REF_SAMPLES_NUM) + (DFE_SAMPLES_NUM))

/** Number of samples in a single sampling slot */
#define DFE_SAMPLES_PER_SLOT_NUM ((CONFIG_BT_CTLR_DFE_SWITCH_SPACING_NS) /  ((CONFIG_BT_CTLR_DFE_SAMPLE_SPACING_NS) * 2))

/** Total number of slots (sampling and switching) during whole CTE */
#define DFE_TOTAL_SLOTS_NUM ((DFE_ANT_SWITCHING_PERIOD_NS) / (CONFIG_BT_CTLR_DFE_SAMPLE_SPACING_NS))
/** Numbe of sampling slot during whole CTE */
#define DFE_SAMPLING_SLOTS_NUM ((DFE_TOTAL_SLOTS_NUM) / 2)

/** @brief Max number of IQ samples that may be stored in single DFE package */
#define DF_DATA_COUNT (512)

struct cte_info {
	u8_t cte_time;
	u8_t rfu;
	u8_t cte_type;
} __attribute__((packed));

/** @brief Single IQ sample storage structure
 *
 * @note Pay attention that samples are stored in 16 bit integer values
 * 	 but values provided by radio are 12 bit length.
 */
struct dfe_iq {
	int16_t i; //!< 12bit
	int16_t q; //!< 12bit
} __attribute__((packed));

/** @brief Single magnitude/phase sample storage structure
 *
 * @note Pay attention that:
 * 	 - magnitude id stored in 16 bit integer value, but radio provides it
 * 	 as 13 bit length value and it equals: 1.646756*sqrt(I^2+Q^2)
 * 	 - phase is stored in 16 bit signed value, but radio provides it as
 * 	 9 bit length value (sign extended to 16bit) and it equals:
 * 	 64*atan2(Q, I) in the range [-201,201]
 */
struct dfe_magphase {
	uint16_t mag;	//!< 13bit = 1.646756*sqrt(i^2+q^2)
	int16_t phase;	//!< 9bit  = 64*atan2(Q,I), range [-201,201]
} __attribute__((packed));

/** @brief Union to store IQ or magnitude/phase representation of samples.
 */
union dfe_data {
	struct dfe_iq iq;
	struct dfe_magphase magphase;
} __attribute__((packed));

/** @brief Header structure of data gathered by DFE
 */
struct dfe_hdr {
	/** number of samples gathered */
	uint16_t length;
	/** frequency used to gather samples */
	uint16_t frequency;
} __attribute__((packed));

/** @brief Data package that holds all samples gathered by DFE
 */
struct dfe_packet {
	struct dfe_hdr hdr;
	union dfe_data data[DFE_TOTAL_SAMPLES_NUM];
} __attribute__((packed));

#endif //__DF_DATA_H__
